/**
 *
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 *
 */

/**
 * CHANGE HiSTORY:
 * Author             Time                 Description
 *
 */
package com.ibm.ba.demo.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.ibm.ba.demo.dao.BatchScoreDao;
import com.ibm.ba.demo.dao.InDBScoreDao;
import com.ibm.ba.demo.dao.ProductDao;
import com.ibm.ba.demo.entity.BaDemoScoreResult;
import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.exception.GeneralException;
import com.ibm.ba.demo.helper.BeanHelper;

@Service
public class InDBBatchScoreServiceImpl implements BatchScoreService {

	private static Logger logger = Logger
			.getLogger(InDBBatchScoreServiceImpl.class);

	@Autowired
	private ProductDao productDao;

	@Autowired
	private BatchScoreDao batchScoreDao;

	@Autowired
	private InDBScoreDao inDBScoreDao;

	@Autowired
	private InDBScoreRequestServiceImpl scoreRequestService;

	@Autowired
	private SPSSModelConfiguration config;

	@Override
	public BaDemoScoreResult getScore(int customerId) throws GeneralException {
		return batchScoreDao.getScore(customerId, 1);
	}

	@Override
	public void runBatchScore() throws GeneralException {

		List<BaDemoScoreResult> baDemoScoreResult = getScore(config
				.getIntegerList(config.getCustomerIds()));
		logger.info("baDemoScoreResult is: "
				+ BeanHelper.beanToJson(baDemoScoreResult));
		try {
			batchScoreDao.batchUpdate(baDemoScoreResult, 1);
		} catch (DataAccessException e) {
			logger.error("ba.error.00006 - Batch update failed[baDemoScoreResult:"
					+ BeanHelper.beanToJson(baDemoScoreResult) + "]", e);
			throw new GeneralException("ba.error.00006", e);
		} catch (Exception e) {
			logger.error("ba.error.00001 - Batch update failed[baDemoScoreResult:"
					+ BeanHelper.beanToJson(baDemoScoreResult) + "]", e);
			throw new GeneralException("ba.error.00001", e);
		}
	}

	@Override
	public List<BaDemoScoreResult> getScore(List<Integer> customerIdList)
			throws GeneralException {
		List<Map<String, Object>> resultList = productDao
				.getProductIdsByCustomerIds(customerIdList);
		List<Map<String, Object>> inputParamList = scoreRequestService
				.convert(resultList);
		List<BaDemoScoreResult> baDemoScoreResultList = null;
		try {
			baDemoScoreResultList = inDBScoreDao.getScore(inputParamList);
		} catch (DataAccessException e) {
			logger.error("ba.error.00007 - query failed[inputParamList:"
					+ BeanHelper.beanToJson(inputParamList) + "]", e);
			throw new GeneralException("ba.error.00007", e);
		} catch (Exception e) {
			logger.error("ba.error.00001 - query failed[inputParamList:"
					+ BeanHelper.beanToJson(inputParamList) + "]", e);
			throw new GeneralException("ba.error.00001", e);
		}
		logger.info("baDemoScoreResultList is: "
				+ BeanHelper.beanToJson(baDemoScoreResultList));
		return baDemoScoreResultList;
	}

}
